package com.google.code.bbdsm.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import blackboard.platform.BbServiceManager;
import blackboard.platform.context.Context;

/**
 * Set up the Blackboard Context, and make it available as a request attribute.
 *
 */
public class BbContextInterceptor extends HandlerInterceptorAdapter {

	protected String requestAttributeKeyForBbContext;

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		Context ctx = BbServiceManager.getContextManager().setContext(request);
		request.setAttribute(requestAttributeKeyForBbContext, ctx);
		return true;
		
	}
	
	/*
	 * It is not known whether or not enabling this method will break any
	 * subsequent JSPs, so it has been left out
	 *
	 *
	 * public void afterCompletion(HttpServletRequest request,
	 * 							   HttpServletResponse response, 
	 * 							   Object handler, 
	 * 							   Exception ex) throws	Exception { 
	 * 		request.removeAttribute(requestAttributeKeyForBbContext);
	 * 		BbServiceManager.getContextManager().purgeContext(); 
	 * }
	 */

	public String getRequestAttributeKeyForBbContext() {
		
		return requestAttributeKeyForBbContext;
	}

	public void setRequestAttributeKeyForBbContext(
			String requestAttributeKeyForBbContext) {
		this.requestAttributeKeyForBbContext = requestAttributeKeyForBbContext;
	}

}
